﻿Imports System.Data
Imports System.Data.OleDb
Imports System.IO

Public Class frmCadPedidos
    Dim OBJCLI As New CLSClientes
    Dim OBJPED As New CLSPedidos
    Dim OBJCONTROLE As New ClsControle
    Dim OBJPedCesta As New CLSPedidoCesta
    Dim total, geral As Single
    Dim estoque As Integer
    Dim sql As String
    Dim objbanco As New CLSBanco
    Dim novo As Boolean

    Private Sub btnPesquisarCPF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPesquisarCPF.Click
        Dim OBJDTLOCAL As New DataTable
        If MskCPF.Text = "   -   -   -" Then
            MessageBox.Show("Digite o CPF")
            MskCPF.Focus()
            Exit Sub
        End If

        OBJDTLOCAL = OBJCLI.LocalizarPorCPF(MskCPF.Text)
        If OBJDTLOCAL.Rows.Count = 0 Then
            MessageBox.Show("Cliente não encontrado")
            MskCPF.Clear()
            txtidCliente.Clear()
            txtNomeCliente.Clear()
        Else
            txtidCliente.Text = OBJDTLOCAL.Rows(0).Item(0)
            txtNomeCliente.Text = OBJDTLOCAL.Rows(0).Item(1)
            txtCred.Text = OBJDTLOCAL.Rows(0).Item(5)
            If txtCred.Text = OBJDTLOCAL.Rows(0).Item(5) = True Then
                txtCred.Text = "Tem Crédito"
            Else
                txtCred.Text = "Não tem Crédito"
                MsgBox("Cliente possui débitos pendentes", vbOKOnly + vbCritical, "Atenção")
            End If

        End If
    End Sub

    Private Sub txtCpf_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = 13 Then
            btnPesquisarCPF.PerformClick()
        End If
    End Sub

    Private Sub btnGeraPedidos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGeraPedidos.Click
     
        If Not OBJCONTROLE.Testar_vazio(Me) Then

            If cboSitPed.Text = "" Then
                MsgBox("SELECIONE A SITUACAO DO PEDIDO", 16)
                Exit Sub
            End If

            OBJPED.ID_Cliente = txtidCliente.Text
            OBJPED.Data_Pedido = MaskDataPed.Text
            OBJPED.Data_Vencimento = MskDatVenc.Text
            OBJPED.Data_Pagamento = MaskDataPagto.Text
            OBJPED.Situação_Pedido = cboSitPed.Text
            OBJPED.valorTotalPedido = txtTotalGeral.Text
            Dim linha As Integer
            novo = True
            OBJPED.Gravar(novo)

            txtCodPedido.Text = OBJPED.ID
            For linha = 0 To dgdGradeItensPedidos.RowCount - 1
                OBJPedCesta.CodigoPedido = OBJPED.ID
                OBJPedCesta.CodigoCesta = dgdGradeItensPedidos.Item(0, linha).Value
                OBJPedCesta.qtdCesta = dgdGradeItensPedidos.Item(3, linha).Value
                OBJPedCesta.Valor = dgdGradeItensPedidos.Item(4, linha).Value
                OBJPedCesta.Gravar()
                MessageBox.Show("Dados do Pedido salvo com Sucesso! Entregar cesta", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Next
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        frmConsultaCesta.ShowDialog()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        dgdGradeItensPedidos.Rows.Add(txtCodCesta.Text, txtNomCesta.Text, txtValorUniCesta.Text, txtQtdCestaPedido.Text, total)
        geral = geral + total
        txtTotalGeral.Text = geral
        txtQtdCestaPedido.Clear()
        txtQtdCestaPedido.Focus()
    End Sub

    Public Sub txtQtd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQtdCestaPedido.TextChanged
        If txtQtdCestaPedido.Text <> "" Then
            total = txtQtdCestaPedido.Text * txtValorUniCesta.Text
        End If
    End Sub

  
    Private Sub data_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        sql = "select max(Cli_Id) from cliente"
        Dim objda As New OleDb.OleDbDataAdapter(sql, objbanco.objcon)
        Dim objds As New DataSet
        objda.Fill(objds, "Cliente, pedidos")
        txtNomeCliente.DataBindings.Add("text", objds.Tables("Cliente,pedidos").DefaultView, "nome")
        txtidCliente.DataBindings.Add("text", objds.Tables("Cliente, pedidos").DefaultView, "codigo")
        txtCodPedido.DataBindings.Add("text", objds.Tables("pedidos, pedidos").DefaultView, "codigopedido")
        txtTotalGeral.DataBindings.Add("text", objds.Tables("pedidos, pedidos").DefaultView, "total")
    End Sub

    Private Sub btnVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizar.Click
        Dim visualForm As New visualizarImprimir
        visualForm.pcodigoPedido = txtCodPedido.Text
        visualForm.cnomeCliente = txtNomeCliente.Text
        visualForm.ccodigoCliente = txtidCliente.Text
        visualForm.cCPF = MskCPF.Text
        visualForm.pDataPed = MaskDataPed.Text
        visualForm.pDataVenc = MskDatVenc.Text
        visualForm.pValorTotal = txtTotalGeral.Text
        visualForm.ccodigoCesta = txtCodCesta.Text
        visualForm.cNomeCesta = txtNomCesta.Text
        visualForm.ShowDialog()

    End Sub

    Private Sub btnSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSair.Click
        Me.Close()
    End Sub

    Private Sub btnAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlterar.Click
        novo = False
    End Sub
End Class
